/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batailledesardennes.game.state;

import batailledesardennes.entities.common.Tree;
import batailledesardennes.entities.testTribe.EntityFactory;
import batailledesardennes.game.GameConstants;
import batailledesardennes.game.entity.TestEntity;
import batailledesardennes.game.GameEnv;
import batailledesardennes.game.entity.BuildingEntity;
import batailledesardennes.game.input.InputHandler;
import batailledesardennes.game.input.KeyEvent;
import batailledesardennes.game.input.MouseEvent;
import batailledesardennes.game.entity.Entity;
import batailledesardennes.game.map.imagemap.ImageBasedGameMap;
import batailledesardennes.game.ui.SelectionDrawable;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author charly
 */
public class TestState extends BasicGameState {
   private final GameEnv env;
   private InputHandler inputHandler;

   public TestState(GameEnv env) {
      this.env = env;
   }

   @Override
   public int getID() {
      return GameConstants.STATE_ID_TEST;
   }

   @Override
   public void init(GameContainer container, StateBasedGame game) throws SlickException {
//      for (int i = 0; i < 5; i++) {

      SelectionDrawable selectionDrawable = new SelectionDrawable();
      env.getUiManager().addUIDrawable(selectionDrawable);
      inputHandler = new InputHandler(env);
      inputHandler.setSelectionDrawable(selectionDrawable);

      ImageBasedGameMap map = new ImageBasedGameMap(1000, 1000);
      map.init(env);
      TestEntity entity = new TestEntity();
      entity.register(env);
//         entity.setPosX(10 + i * 60);
//      }

      BuildingEntity woodCutterHouse = EntityFactory.buildWoodCutterHouse();
      woodCutterHouse.getPos().translateTo(200, 200);
      woodCutterHouse.register(env);
      
      Tree tree = batailledesardennes.entities.common.EntityFactory.buildTree();
      tree.getPos().setPos(100, 100);
      tree.register(env);
   }

   @Override
   public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
//      g.pushTransform();
//      g.scale(3,3);
      g.pushTransform();
      g.translate(-env.getViewPortManager().getGameLeftX(), -env.getViewPortManager().getGameTopY());
      env.getMap().draw(g);
      for (Entity e : env.getEntitiesManager().getVisibleEntities()) {
         e.draw(g);
      }
//      g.popTransform();
      g.popTransform();
      env.getUiManager().draw(g);
   }

   @Override
   public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
      env.getUpdatableManager().update(delta);
      inputHandler.update(delta);
      env.getEntitiesManager().update();
   }

   @Override
   public void mousePressed(int button, int x, int y) {
      MouseEvent event = new MouseEvent();
      event.setType(MouseEvent.Type.PRESSED);
      event.setDown(true);
      event.setX(x);
      event.setY(y);
      switch (button) {
         case Input.MOUSE_LEFT_BUTTON:
            event.setButton(MouseEvent.Button.LEFT);
            break;
         case Input.MOUSE_MIDDLE_BUTTON:
            event.setButton(MouseEvent.Button.MIDDLE);
            break;
         case Input.MOUSE_RIGHT_BUTTON:
            event.setButton(MouseEvent.Button.RIGHT);
            break;
      }
      inputHandler.processMouseEvent(event);
   }

   @Override
   public void mouseReleased(int button, int x, int y) {
      MouseEvent event = new MouseEvent();
      event.setType(MouseEvent.Type.RELEASED);
      event.setDown(false);
      event.setX(x);
      event.setY(y);
      switch (button) {
         case Input.MOUSE_LEFT_BUTTON:
            event.setButton(MouseEvent.Button.LEFT);
            break;
         case Input.MOUSE_MIDDLE_BUTTON:
            event.setButton(MouseEvent.Button.MIDDLE);
            break;
         case Input.MOUSE_RIGHT_BUTTON:
            event.setButton(MouseEvent.Button.RIGHT);
            break;
      }
      inputHandler.processMouseEvent(event);
   }

   @Override
   public void mouseDragged(int oldx, int oldy, int newx, int newy) {
      MouseEvent event = new MouseEvent();
      event.setType(MouseEvent.Type.MOVED);
      event.setDown(true);
      event.setX(newx);
      event.setY(newy);
      event.setButton(null);
      inputHandler.processMouseEvent(event);
   }

   @Override
   public void keyPressed(int key, char c) {
      KeyEvent keyEvent = new KeyEvent(key, true);
      inputHandler.processKeyEvent(keyEvent);
   }

   @Override
   public void keyReleased(int key, char c) {
      KeyEvent event = new KeyEvent(key, false);
      inputHandler.processKeyEvent(event);
   }
}
